package com.tangly.module.controller;

import com.github.pagehelper.PageInfo;
import com.tangly.common.base.BaseController;
import com.tangly.common.utils.ValidateUtil;
import com.tangly.module.entity.CmsArticleEntity;
import com.tangly.module.service.ICmsArticleService;
import com.tangly.module.vo.AppCmsArticleVO;
import com.tangly.module.vo.ResultVO;
import com.tangly.module.vo.SearchRequestVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.Result;
import java.util.ArrayList;
import java.util.List;

/**
 * created by yehao on 2018/7/26.
 */
@RestController
@RequestMapping(value = "/app/home")
@Slf4j
@Api(tags = "资讯")
public class AppCmsArticleController extends BaseController {

    @Autowired
    private ICmsArticleService iCmsArticleService;

    @ApiOperation(value = "获取资讯列表", notes = "获取列表,在url中使用?page=xx&size=xx实现分页,不传默认是1,10")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", paramType = "query", value = "页码(不传默认是1)", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "size", paramType = "query", value = "页面大小(不传默认是10)", dataType = "int", defaultValue = "10"),
            @ApiImplicitParam(name = "sorts", paramType = "query", value = "排序字段，排序条件 例：[+id,-name]表示根据id 正序排列并根据name逆向排序;"),
            @ApiImplicitParam(name = "keyword", paramType = "query", value = "查询关键字,通常会根据name字段模糊查询")
    })
    @GetMapping(value = "article")
    public ResultVO<AppCmsArticleVO> getList(
            @RequestParam(name = "page", defaultValue = "1", required = false) Integer page,
            @RequestParam(name = "size", defaultValue = "10", required = false) Integer size,
            @RequestParam(name = "sorts", required = false) String[] sorts,
            @RequestParam(name = "keyword", required = false) String keyword
    ){
        SearchRequestVO searchRequestVO = new SearchRequestVO(page,size,sorts);
        if (!StringUtils.isEmpty(keyword)){
            searchRequestVO.addColumn("summary","%"+keyword+"%");
        }
        PageInfo<CmsArticleEntity> pages=iCmsArticleService.selectOrByPage(searchRequestVO,CmsArticleEntity.class);
        List<AppCmsArticleVO> list = new ArrayList<>();
        for (CmsArticleEntity cmsArticleEntity:pages.getList()){
            list.add(AppCmsArticleVO.convert(cmsArticleEntity));
        }

        return new ResultVO<>(pages.getTotal(), pages.getPages(), list);
    }

    @ApiOperation(value = "获取资讯详情",notes = "根据url的id来获取CmsArticle详细信息")
    @ApiImplicitParam(paramType = "path",name = "id",dataType = "Integer")
    @GetMapping(value = "article/{id}")
    public ResultVO<AppCmsArticleVO> getCmsArticle(@PathVariable("id") Integer id){
        CmsArticleEntity cmsArticleEntity =iCmsArticleService.selectByPrimaryKey(id);
        AppCmsArticleVO vo=AppCmsArticleVO.convert(cmsArticleEntity);
        return new ResultVO<>(vo);
    }


    @ApiOperation(value ="创建CmsArticle实体",notes = "根据CmsArticle对象创建CmsArticle")
    @ApiImplicitParam(name = "appCmsArticleVO", value = "id字段由服务端生成，前端不用传", required = true, dataType = "AppCmsArticleVO")
    @PostMapping(value = "insertarticle")
    public ResultVO<String> postCmsArticle(@RequestBody AppCmsArticleVO appCmsArticleVO){
        ValidateUtil.validate(appCmsArticleVO);
        CmsArticleEntity cmsArticleEntity = new CmsArticleEntity();
        BeanUtils.copyProperties(appCmsArticleVO,cmsArticleEntity);
        iCmsArticleService.insert(cmsArticleEntity);
        return  ResultVO.success("创建成功");
    }
}
